์คํ๋ง ํต์ฌ ์๋ฆฌ - ๊ธฐ๋ณธํธ์ ๋ค์ผ๋ฉฐ ์ ๋ฆฌํ๋ POST์ ๋๋ค.
๐ฏ ์น ์ ํ๋ฆฌ์ผ์ด์ ๊ณผ ์ฑ๊ธํค
๋๋ถ๋ถ์ ์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์ ์ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ด๋ค. (๋ฌผ๋ก ์น์ด ์๋ ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ๋ ์ผ๋ง๋ ์ง ๊ฐ๋ฐ ๊ฐ๋ฅํ๋ค.)
- ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ณดํต ์ฌ๋ฌ ๊ณ ๊ฐ์ด ๋์์ ์์ฒญ์ ํ๋ค.
- ์๋ก์ด ํด๋ผ์ด์ธํธ๊ฐ ์์ฒญํ ๋๋ง๋ค ์ธ์คํด์ค๋ฅผ ์๋ก ์์ฑํ์ฌ ๋ฐํํ๋ค.
-
๋ฐ๋ผ์, ๋งค ์์ฒญ๋ง๋ค ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ ์์ฒญ ์๋งํผ ์์ฑ๋๋ค.
- ์ด์ ์ ์ํํ๋
AppConfig๋ฅผ ์ด์ฉํ ๋ฐฉ์์ด ์ด์ ๊ฐ๋ค.
- ์ด์ ์ ์ํํ๋
์คํ๋ง ์๋ ์์ํ DI ์ปจํ ์ด๋ ํ ์คํธ
package hello.core.singleton;
import hello.core.AppConfig;
import hello.core.member.MemberService;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
public class SingletonTest {
@Test
@DisplayName("์คํ๋ง ์๋ ์์ํ DI ์ปจํ
์ด๋")
void pureContainer() {
AppConfig appConfig = new AppConfig();
// 1. ์กฐํ: ํธ์ถํ ๋๋ง๋ค ๊ฐ์ฒด๋ฅผ ์์ฑ
MemberService memberService1 = appConfig.memberService();
// 2. ์กฐํ: ํธ์ถํ ๋๋ง๋ค ๊ฐ์ฒด๋ฅผ ์์ฑ
MemberService memberService2 = appConfig.memberService();
// ์ฐธ์กฐ๊ฐ์ด ๋ค๋ฅธ์ง ํ์ธ
System.out.println("memberService1 = " + memberService1);
System.out.println("memberService2 = " + memberService2);
assertThat(memberService1).isNotSameAs(memberService2);
}
}
- ์ด์ ์ ๋ง๋ ์คํ๋ง ์๋ ์์ํ DI ์ปจํ
์ด๋์ธ
AppConfig๋ ์์ฒญ๋ง๋ค ๊ฐ์ฒด๋ฅผ ์๋ก ์์ฑํ๋ค. - ๊ณ ๊ฐ ํธ๋ํฝ์ด ์ด๋น 100์ด๋ผ๊ณ ๊ฐ์ ํ๋ฉด, ์ด๋น 100๊ฐ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ์๋ฉธํ๋ค. ์ด๋ ์ฌ๊ฐํ ๋ฉ๋ชจ๋ฆฌ ๋ญ๋น๋ฅผ ์ด๋ํ๋ค.
์ด์ ๋ํ ํด๊ฒฐ๋ฐฉ์์ผ๋ก ํด๋น ๊ฐ์ฒด๊ฐ ๋ฑ 1๊ฐ๋ง ์์ฑ๋๊ณ , ์ด๋ฅผ ๊ณต์ ํ๋๋ก ์ค๊ณํ๋ค. -> ์ฑ๊ธํค ํจํด
๐ฏ ์ฑ๊ธํค ํจํด
ํด๋์ค์ ์ธ์คํด์ค๊ฐ ๋ฑ 1๊ฐ๋ง ์์ฑ๋๋ ๊ฒ์ ๋ณด์ฅํ๋ ๋์์ธ ํจํด์ด๋ค.
-
๊ฐ์ฒด ์ธ์คํด์ค๋ฅผ 2๊ฐ ์ด์ ์์ฑํ์ง ๋ชปํ๋๋ก ๋ง์์ผ ํ๋ค!
private์์ฑ์๋ฅผ ์ด์ฉํด ์ธ๋ถ์์ ์์๋กnewํค์๋๋ฅผ ์ฌ์ฉํ์ง ๋ชปํ๋๋ก ๋ง์์ผ ํ๋ค.
package hello.core.singleton;
public class SingletonService {
// ์๊ธฐ ์์ ์ ๋ด๋ถ์ private์ผ๋ก ๊ฐ์ง
// ์ด๋ static์ผ๋ก ๊ฐ์ง๊ธฐ์ ํด๋์ค ๋ ๋ฒจ์ ๋ฑ 1๊ฐ๋ง ์์ฑ๋๋ค.
private static final SingletonService instance = new SingletonService();
public static SingletonService getInstance() {
// ๋ด๋ถ์ ์ผ๋ก new SingletonService๋ฅผ ์คํํด ์ฐธ์กฐ๊ฐ์ ๋ฃ์ด๋์ instance๋ฅผ ๋ฐํ
return instance;
}
}
- ์์ ๊ฐ์ด
getInstance()๋ฅผ ํตํด ์์ฑํด๋์SingletonService๋ฅผ ๋ฐํ๋ฐ์ ์ ์๋๋ก ๊ตฌํํ๋ค. - ํ์ง๋ง ์ธ๋ถ์์ ์์๋ก
new SingletonService()๋ฅผ ํธ์ถํ๋ค๋ฉด, ์ฌ๋ฌ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๊ฒ ๋๋ค.
package hello.core.singleton;
public class SingletonService {
// ์๊ธฐ ์์ ์ ๋ด๋ถ์ private์ผ๋ก ๊ฐ์ง
// ์ด๋ static์ผ๋ก ๊ฐ์ง๊ธฐ์ ํด๋์ค ๋ ๋ฒจ์ ๋ฑ 1๊ฐ๋ง ์์ฑ๋๋ค.
private static final SingletonService instance = new SingletonService();
public static SingletonService getInstance() {
// ๋ด๋ถ์ ์ผ๋ก new SingletonService๋ฅผ ์คํํด ์ฐธ์กฐ๊ฐ์ ๋ฃ์ด๋์ instance๋ฅผ ๋ฐํ
return instance;
}
private SingletonService() {
}
public void logic() {
System.out.println("์ฑ๊ธํค ๊ฐ์ฒด ๋ก์ง ํธ์ถ");
}
}
static์์ญ์ ๊ฐ์ฒดinstance๋ฅผ ๋ฏธ๋ฆฌ ํ๋ ์์ฑํด ์ฌ๋ ค๋๋ค.- ์ด ๊ฐ์ฒด ์ธ์คํด์ค๊ฐ ํ์ํ๋ฉด ์ค์ง
getInstance()๋ฉ์๋๋ฅผ ํตํด์๋ง ์กฐํํ ์ ์๋ค. ์ด ๋ฉ์๋๋ ํญ์ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ๋ฐํํ๋ค. - ๋ฑ 1๊ฐ์ ๊ฐ์ฒด ์ธ์คํด์ค๋ง ์กด์ฌํด์ผ ํ๋ฏ๋ก, ์์ฑ์๋ฅผ
private์ผ๋ก ๋ง์ ํน์๋ผ๋ ์ธ๋ถ์์newํค์๋๋ก ๊ฐ์ฒด ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ ๊ฒ์ ๋ง๋๋ค.
์ฑ๊ธํค ํจํด์ ์ฌ์ฉํ๋ ํ ์คํธ ์ฝ๋
@Test
@DisplayName("์ฑ๊ธํค ํจํด์ ์ ์ฉํ ๊ฐ์ฒด ์ฌ์ฉ")
void singletonServiceTest() {
SingletonService singletonService1 = SingletonService.getInstance();
SingletonService singletonService2 = SingletonService.getInstance();
System.out.println("singletonService1 = " + singletonService1);
System.out.println("singletonService2 = " + singletonService2);
assertThat(singletonService1).isSameAs(singletonService2);
// same ==
// equal
}
private์ผ๋กnewํค์๋๋ฅผ ๋ง์๋์๋ค.- ํธ์ถ๋ง๋ค ๊ฐ์ ๊ฐ์ฒด ์ธ์คํด์ค๋ฅผ ๋ฐํํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ฑ๊ธํค ํจํด์ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ๋งค์ฐ ๋ง๋ค. ์ฌ๊ธฐ์๋ ๊ฐ์ฒด๋ฅผ ๋ฏธ๋ฆฌ ์์ฑํด๋๋ ๊ฐ์ฅ ๋จ์ํ๊ณ ์์ ํ ๋ฐฉ๋ฒ์ ๊ตฌํํ๋ค.
์ด๋ฏธ ๋ง๋ค์ด์ง ๊ฐ์ฒด๋ฅผ ๊ณต์ ํด์ ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ง๋ง, ์ฑ๊ธํค ํจํด์ ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ์ ๋ค์ ๊ฐ์ง๊ณ ์๋ค.
- ์ฑ๊ธํค ํจํด์ ๊ตฌํํ๋ ์ฝ๋ ์์ฒด๊ฐ ๋ง์ด ๋ค์ด๊ฐ๋ค.
- ์์กด๊ด๊ณ์ ํด๋ผ์ด์ธํธ๊ฐ ๊ตฌ์ฒด ํด๋์ค์ ์์กดํ๋ค. -> DIP ์๋ฐ!!!
- ํด๋ผ์ด์ธํธ๊ฐ ๊ตฌ์ฒด ํด๋์ค์ ์์กดํด์ OCP ์์น์ ์๋ฐํ ๊ฐ๋ฅ์ฑ์ด ๋๋ค.
- ํ ์คํธ๊ฐ ์ด๋ ต๋ค.
- ๋ด๋ถ ์์ฑ์ ๋ณ๊ฒฝํ๊ฑฐ๋ ์ด๊ธฐํํ๊ธฐ ์ด๋ ต๋ค.
private์์ฑ์๋ก ์ธํด ์์ ํด๋์ค๋ฅผ ๋ง๋ค๊ธฐ ์ด๋ ต๋ค.- ๊ฒฐ๋ก ์ ์ผ๋ก ์ ์ฐ์ฑ์ด ๋จ์ด์ง๋ค. (DI์ ์ ์ฉ์ ์ด๋ ต๊ฒ ํจ)
- ์ํฐ ํจํด์ผ๋ก ๋ถ๋ฆฌ๊ธฐ๋ ํ๋ค.
๐ฏ ์ฑ๊ธํค ์ปจํ ์ด๋
์คํ๋ง ์ปจํ ์ด๋๋ ์ฑ๊ธํค ํจํด์ ๋ฌธ์ ์ ์ ํด๊ฒฐํ๋ฉด์, ๊ฐ์ฒด ์ธ์คํด์ค๋ฅผ ์ฑ๊ธํค(1๊ฐ๋ง ์์ฑ)์ผ๋ก ๊ด๋ฆฌํ๋ค.
- ์ง๊ธ๊น์ง ๋ฐฐ์ด ์คํ๋ง ๋น์ด ๋ฐ๋ก ์ฑ๊ธํค์ผ๋ก ๊ด๋ฆฌ๋๋ ๋น์ด๋ค!
๐ช ์ฑ๊ธํค ์ปจํ ์ด๋
-
์คํ๋ง ์ปจํ ์ด๋๋ ์ฑ๊ธํค ํจํด์ ์ ์ฉํ์ง ์์๋, ๊ฐ์ฒด ์ธ์คํด์ค๋ฅผ ์ฑ๊ธํค์ผ๋ก ๊ด๋ฆฌํ๋ค.
- ์ด์ ์ ๋ค๋ฃฌ ์ปจํ ์ด๋ ์์ฑ ๊ณผ์ ์ ์์ธํ ๋ณด๋ฉด, ์ปจํ ์ด๋๋ ๊ฐ์ฒด๋ฅผ ํ๋๋ง ์์ฑํด์ ๊ด๋ฆฌํ๋ค.
- ์คํ๋ง ์ปจํ ์ด๋๋ ์ฑ๊ธํค ์ปจํ ์ด๋ ์ญํ ์ ํ๋ค. ์ด๋ ๊ฒ ์ฑ๊ธํค ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ ๊ด๋ฆฌํ๋ ๊ธฐ๋ฅ์ ์ฑ๊ธํค ๋ ์ง์คํธ๋ฆฌ๋ผ ํ๋ค.
-
์คํ๋ง ์ปจํ ์ด๋์ ์ด๋ฐ ๊ธฐ๋ฅ๋์ ์ฑ๊ธํค ํจํด์ ๋ชจ๋ ๋จ์ ์ ํด๊ฒฐํ๋ฉด์ ๊ฐ์ฒด๋ฅผ ์ฑ๊ธํค์ผ๋ก ์ ์งํ ์ ์๋ค.
- ์ง์ ๋ถํ ์ฝ๋๊ฐ ๋ค์ด๊ฐ์ง ์์๋ ๋๋ค.
- DIP, OCP, ํ
์คํธ,
private์์ฑ์๋ก๋ถํฐ ์์ ๋กญ๊ฒ ์ฑ๊ธํค์ ์ฌ์ฉํ ์ ์๋ค.
์คํ๋ง ์ปจํ ์ด๋๋ฅผ ์ฌ์ฉํ๋ ํ ์คํธ ์ฝ๋
@Test
@DisplayName("์คํ๋ง ์ปจํ
์ด๋์ ์ฑ๊ธํค")
void springContainer() {
// AppConfig appConfig = new AppConfig();
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
// 1. ์กฐํ: ํธ์ถํ ๋๋ง๋ค ๊ฐ์ฒด๋ฅผ ์์ฑ
// MemberService memberService1 = appConfig.memberService();
MemberService memberService1 = ac.getBean("memberService", MemberService.class);
// 2. ์กฐํ: ํธ์ถํ ๋๋ง๋ค ๊ฐ์ฒด๋ฅผ ์์ฑ
MemberService memberService2 = ac.getBean("memberService", MemberService.class);
// MemberService memberService2 = appConfig.memberService();
// ์ฐธ์กฐ๊ฐ์ด ๊ฐ์์ง ํ์ธ
System.out.println("memberService1 = " + memberService1);
System.out.println("memberService2 = " + memberService2);
assertThat(memberService1).isSameAs(memberService2);
}
-
์คํ๋ง ์ปจํ ์ด๋์ ๋ฑ๋กํ ๋น์ด ๊ณ์ ์กฐํ๋์ด ๊ฐ์ ์ฐธ์กฐ๊ฐ์ ๊ฐ์ง๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
- ๊ทธ๋ฆฌ๊ณ
MemberService์ฝ๋๋ฅผ ํ์ธํด๋ณด์๋, ์ฑ๊ธํค๊ณผ ๊ด๋ จ๋ ์ฝ๋๋ ํ๋๋ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
- ๊ทธ๋ฆฌ๊ณ
์ฑ๊ธํค ์ปจํ
์ด๋ ์ ์ฉ ํ
- ์คํ๋ง ์ปจํ ์ด๋ ๋๋ถ์ ๊ณ ๊ฐ์ ์์ฒญ๋ง๋ค ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ฒ์ด ์๋ ์ด๋ฏธ ๋ง๋ค์ด์ง ๊ฐ์ฒด๋ฅผ ๊ณต์ ํด์ ํจ์จ์ ์ธ ์ฌ์ฌ์ฉ์ด ๊ฐ๋ฅํด์ก๋ค.
์คํ๋ง์ ๊ธฐ๋ณธ ๋น ๋ฑ๋ก ๋ฐฉ์์ ์ฑ๊ธํค์ด์ง๋ง, ์ฑ๊ธํค ๋ฐฉ์๋ง ์ง์ํ๋ ๊ฒ์ ์๋๋ค.
- ์์ฒญ๋ง๋ค ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํด์ ๋ฐํํ๋ ๊ธฐ๋ฅ๋ ์ ๊ณตํ๊ธด ํ๋ค!
๐ฏ ์ฑ๊ธํค ๋ฐฉ์์ ์ฃผ์์
์ฑ๊ธํค ํจํด์ด๋ , ์คํ๋ง๊ณผ ๊ฐ์ ์ฑ๊ธํค ์ปจํ ์ด๋๋ฅผ ์ฌ์ฉํ๋ , ๊ฐ์ฒด ์ธ์คํด์ค๋ฅผ ํ๋๋ง ์์ฑํด์ ๊ณต์ ํ๋ ์ฑ๊ธํค ๋ฐฉ์์ ์ฌ๋ฌ ํด๋ผ์ด์ธํธ๊ฐ ํ๋์ ๊ฐ์ ๊ฐ์ฒด ์ธ์คํด์ค๋ฅผ ๊ณต์ ํ๊ธฐ์, ์ฑ๊ธํค ๊ฐ์ฒด๋ ์ํ๋ฅผ ์ ์งํ๊ฒ ์ค๊ณํ๋ฉด ์๋๋ค!
-
์ฆ, ๋ฌด์ํ(stateless)๋ก ์ค๊ณํด์ผ ํ๋ค!
- ํน์ ํด๋ผ์ด์ธํธ์ ์์กด์ ์ธ ํ๋๊ฐ ์์ผ๋ฉด ์๋๋ค! (ํน์ ํด๋ผ์ด์ธํธ๊ฐ ๊ฐ์ ๋ฐ๊พธ๊ฒ ๋๋ฉด ์๋๋ค!)
- ํน์ ํด๋ผ์ด์ธํธ๊ฐ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ ํ๋๊ฐ ์์ผ๋ฉด ์๋๋ค!
- ๊ฐ๊ธ์ ์ฝ๊ธฐ๋ง ๊ฐ๋ฅํด์ผ ํ๋ค. (๊ฐ์ ์์ ์ด ์๋๋ก ํด์ผ ํ๋ค.)
- ํ๋ ๋์ ์ ์๋ฐ์์ ๊ณต์ ๋์ง ์๋, ์ง์ญ๋ณ์, ํ๋ผ๋ฏธํฐ, ThreadLocal ๋ฑ์ ์ฌ์ฉํด์ผ ํ๋ค.
- ์คํ๋ง ๋น์ ํ๋์ ๊ณต์ ๊ฐ์ ์ค์ ํ๋ฉด ์ ๋ง ํฐ ์ฅ์ ๊ฐ ๋ฐ์ํ ์ ์๋ค..!
์ํ๋ฅผ ์ ์งํ ๊ฒฝ์ฐ ๋ฐ์ํ๋ ๋ฌธ์ ์ ์์
package hello.core.singleton;
public class StatefulService {
// ์ํ๋ฅผ ์ ์งํ๋ ํ๋
private int price;
public void order(String name, int price) {
System.out.println("name = " + name + " price = " + price);
// ์ฌ๊ธฐ๊ฐ ๋ฌธ์ !!!
this.price = price;
}
public int getPrice() {
return price;
}
}
//
package hello.core.singleton;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import static org.assertj.core.api.Assertions.*;
class StatefulServiceTest {
@Test
void statefulServiceSingleton() {
// TestConfig๋ก ac ์์ฑ
ApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);
StatefulService statefulService1 = ac.getBean(StatefulService.class);
StatefulService statefulService2 = ac.getBean(StatefulService.class);
// ThreadA: A ์ฌ์ฉ์๊ฐ 10000์ ์ฃผ๋ฌธ
statefulService1.order("userA", 10000);
// ThreadB: B ์ฌ์ฉ์๊ฐ 20000์ ์ฃผ๋ฌธ
statefulService2.order("userB", 20000);
// ThreadA: A ์ฌ์ฉ์๊ฐ ์ฃผ๋ฌธ ๊ธ์ก ์กฐํ -> ๊ณผ์ฐ ์ผ๋ง๊ฐ ๋์ฌ๊น?
int price = statefulService1.getPrice();
System.out.println("price = " + price);
assertThat(statefulService1.getPrice()).isEqualTo(20000);
}
static class TestConfig {
@Bean
public StatefulService statefulService() {
return new StatefulService();
}
}
}
-
StatefulServiceTest์ ์ถ๋ ฅ๋ฌธ์์ ๊ธฐ๋ํ ๊ฐ์ 10000์์ด๋ค.- ์ฌ์ฉ์ A๊ฐ ์ฃผ๋ฌธํ ๊ธ์ก์ด 10000์์ด๊ณ , ์ด๋ฅผ ๋ค์ ์กฐํํ ๊ฒ์ด๋ฏ๋ก 10000์์ ๊ธฐ๋ํ๋ค.
- ํ์ง๋ง, ์ค๊ฐ์ ์ฌ์ฉ์ B์ ์ฃผ๋ฌธ์ด ๋ผ์ด๋ค์ด ๊ธฐ๋์ ๋ค๋ฅธ ๊ฐ์ด ์ถ๋ ฅ๋์๋ค. (์ฌ์ฉ์ B์ ์ฃผ๋ฌธ์ด
StatefulService์priceํ๋๋ฅผ 20000์ผ๋ก ๋ณ๊ฒฝ!!!) - ๊ทธ๋ฆฌ๊ณ ,
statefulService1๊ณผstatefulService2๋ก ๋ค๋ฅธ ์ธ์คํด์ค๋ฅผ ์ฌ์ฉํ๋คํด๋, ์ฌ์ค ๋์ ๊ฐ์ ์ฐธ์กฐ๊ฐ์ ๊ฐ๋ ์ธ์คํด์ค์ด๊ธฐ์ ๋์ผํ๋ค.
- ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด
order๋ฅผintํ์ผ๋ก ๋ณํํ๊ณ ,priceํ๋ ์ ๊ฑฐ,price๋ฅผ ๋ฐํํ๋ ํํ๋ก ๋ฐ๊ฟ ์ ์๋ค.
๐ฏ @Configuration๊ณผ ์ฑ๊ธํค
@Configuration์ ์ฑ๊ธํค์ ์ํด ์กด์ฌํ๋ค.
AppConfig ๋ฅผ ๋ณด๋ฉด ์ด์ํ ์ ์ด ์๋ค.
package hello.core;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.MemberRepository;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import hello.core.member.MemoryMemberRepository;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
// @Bean memberService -> new MemoryMemberRepository()
// @Bean orderService -> new MemoryMemberRepository()
@Bean
public MemberService memberService() {
return new MemberServiceImpl(memberRepository());
}
@Bean
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
@Bean
public OrderService orderService() {
return new OrderServiceImpl(
memberRepository(),
discountPolicy()
);
}
...
}
-
memberService๋น์ ๋ง๋๋ ์ฝ๋๋ฅผ ๋ณด๋ฉด,memberRepository()๋ฅผ ํธ์ถํ๋ค.- ์ด๋
new MemoryMemberRepository()๋ฅผ ํธ์ถํ๋ค.
- ์ด๋
-
orderService๋น์ ๋ง๋๋ ์ฝ๋๋ฅผ ๋ณด๋ฉด, ๋์ผํ๊ฒmemberRepository()๋ฅผ ํธ์ถํ๋ค.- ์ด๋
new MemoryMemberRepository()๋ฅผ ํธ์ถํ๋ค.
- ์ด๋
๊ฒฐ๊ณผ์ ์ผ๋ก ๊ฐ๊ฐ ๋ค๋ฅธ 2๊ฐ์ MemoryMemberRepository ๊ฐ ์์ฑ๋๋ฉด์ ์ฑ๊ธํค์ด ๊นจ์ง๋ ๊ฒ์ฒ๋ผ ๋ณด์ธ๋ค!!!
- ์คํ๋ง ์ปจํ ์ด๋๋ ์ด๋ฅผ ์ด๋ป๊ฒ ํด๊ฒฐํ ๊น?
๊ฒ์ฆ ์ฉ๋์ ํ ์คํธ ์ฝ๋ ์ถ๊ฐ
memberService์orderService์์ ์์ฑํ๋memberRepository์ ๊ตฌ์ฒด ํด๋์ค๊ฐ ๊ฐ์ ๊ฒ์ฒ๋ผ ๋ณด์ธ๋ค.- ๋ฐ๋ผ์ ๋ ๊ตฌํ ํด๋์ค์์ ํ
์คํธ ์ฉ์ผ๋ก
MemberRepository๋ฅผ ์กฐํํ ์ ์๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ค.
package hello.core.member;
public class MemberServiceImpl implements MemberService {
private final MemberRepository memberRepository;
// ํ
์คํธ ์ฉ๋
public MemberRepository getMemberRepository() {
return memberRepository;
}
}
//
package hello.core.order;
public class OrderServiceImpl implements OrderService {
// ํ์์ ์ฐพ๊ธฐ ์ํด ํ์
private final MemberRepository memberRepository;
// ํ
์คํธ ์ฉ๋
public MemberRepository getMemberRepository() {
return memberRepository;
}
}
ํ ์คํธ ์ฝ๋
package hello.core.singleton;
import hello.core.AppConfig;
import hello.core.member.MemberRepository;
import hello.core.member.MemberServiceImpl;
import hello.core.order.OrderServiceImpl;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ConfigurationSingletonTest {
@Test
void configurationTest() {
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
MemberServiceImpl memberService = ac.getBean(MemberServiceImpl.class);
OrderServiceImpl orderService = ac.getBean(OrderServiceImpl.class);
MemberRepository memberRepository = ac.getBean("memberRepository", MemberRepository.class);
MemberRepository memberRepository1 = memberService.getMemberRepository();
MemberRepository memberRepository2 = orderService.getMemberRepository();
// ๋ชจ๋ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๊ณ ์๋ค.
System.out.println("memberService -> memberRepository = " + memberRepository1);
System.out.println("orderService -> memberRepository = " + memberRepository2);
System.out.println("memberRepository = " + memberRepository);
}
}
- ๋ชจ๋ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๊ณ ์๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
-
๊ฐ๊ฐ ์๋ก
new MemoryMemberRepository()๋ฅผ ํธ์ถํ๋๋ฐ ์ด๋ป๊ฒ ๊ฐ๋ฅํ ๊ฒ์ผ๊น..?memberService(),@Bean์ผ๋ก ์ธํ ๋น ๋ฑ๋ก ์ ํธ์ถ,orderService()๋ก ์ด 3๋ฒ์ ํธ์ถ์ด ๋ ํ ๋ฐ .. ?!AppConfig์์ ํธ์ถ๋ง๋ค ์ถ๋ ฅ๋ฌธ์ ์์ฑํด ์คํํ๋ฉด ์ถ๋ ฅ ๊ฒฐ๊ณผ๋ ๋ชจ๋ 1๋ฒ๋ง ํธ์ถ๋์ด ์ด 3๊ฐ์ ์ถ๋ ฅ๋ฌธ์ ํ์ธํ ์ ์๋ค.memberService() -> memberRepository()memberRepository()orderService() -> memberRepository()- ์ด ์ถ๋ ฅ๋ฌธ์ด 5๊ฐ๊ฐ ๋์์ผ ํ๋๋ฐ ..?
์คํ๋ง ์ปจํ ์ด๋๋ ์ด๋ป๊ฒ๋ ์ฑ๊ธํค ๋ฐฉ์์ ๋ณด์ฅํ๋๊ตฌ๋..!
๐ฏ @Configuration๊ณผ ๋ฐ์ดํธ์ฝ๋ ์กฐ์์ ๋ง๋ฒ
์คํ๋ง ์ปจํ ์ด๋๋ ์ฑ๊ธํค ๋ ์ง์คํธ๋ฆฌ๋ค. ๋ฐ๋ผ์ ์คํ๋ง ๋น์ด ์ฑ๊ธํค์ด ๋๋๋ก ๋ณด์ฅํด์ผ ํ๋ค!
-
ํ์ง๋ง ์คํ๋ง์ด ์๋ฐ ์ฝ๋๊น์ง ์ด๋ป๊ฒ ํ ์๋ ์๋ค.
- ์์์ ์ ์๋ฐ ์ฝ๋๋ง ๋ดค์๋, ๋ถ๋ช
memberRepository()๋ 3๋ฒ ํธ์ถ๋์ด์ผ ํ๋ ๊ฒ์ด ๋ง๋ค.
- ์์์ ์ ์๋ฐ ์ฝ๋๋ง ๋ดค์๋, ๋ถ๋ช
- ๊ทธ๋์ ์คํ๋ง์ ํด๋์ค์ ๋ฐ์ดํธ์ฝ๋๋ฅผ ์กฐ์ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ค.
@Configuration์ ์ ์ฉํAppConfig๋ฅผ ํ์ธํ๋ค.
@Test
void configurationDeep() {
// AppConfig ๋ํ ๋น์ผ๋ก ๋ฑ๋ก๋จ!!!
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
AppConfig bean = ac.getBean(AppConfig.class);
System.out.println("bean = " + bean.getClass());
// ์ถ๋ ฅ: bean = class hello.core.AppConfig$$EnhancerBySpringCGLIB$$79b7202a
}
-
๋น์ผ๋ก ๋ฑ๋ก๋
AppConfig์ ํด๋์ค ํ์ ์ ์ถ๋ ฅํ๋ฉด,EnhancerBySpringCGLIB์ด๋ผ๋ ์ ๋ณด๊ฐ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.- ์์ํ ํด๋์ค๋ผ๋ฉด,
class hello.core.AppConfig๊ฐ ์ถ๋ ฅ๋์ด์ผ ํ๋ค.
- ์์ํ ํด๋์ค๋ผ๋ฉด,
- ์ด๋ ๋ด๊ฐ ๋ง๋ ํด๋์ค๊ฐ ์๋๋ผ, ์คํ๋ง์ด
CGLIB๋ผ๋ ๋ฐ์ดํธ์ฝ๋ ์กฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํดAppConfigํด๋์ค๋ฅผ ์์๋ฐ์ ์์์ ๋ค๋ฅธ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ , ์ด๋ฅผ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํ ๊ฒ์ด๋ค!!!
- ๊ทธ๋ฆฌ๊ณ ์ด ์์์ ํด๋์ค๊ฐ ์ฑ๊ธํค์ด ๋ณด์ฅ๋๋๋ก ํ๋ค.
- ๊ทธ๋ฆฌ๊ณ ์ด ๊ณผ์ ์
@Configuration์ด๋ ธํ ์ด์ ์ ํตํด ์ํ๋๋ค.
AppConfig@CGLIB ์์ ์ฝ๋
@Bean
public MemberRepository memberRepository() {
if (memoryMemberRepository๊ฐ ์ด๋ฏธ ์คํ๋ง ์ปจํ
์ด๋์ ๋ฑ๋ก๋์ด ์์ผ๋ฉด?) {
return ์คํ๋ง ์ปจํ
์ด๋์์ ์ฐพ์์ ๋ฐํ;
} else { //์คํ๋ง ์ปจํ
์ด๋์ ์์ผ๋ฉด
๊ธฐ์กด ๋ก์ง์ ํธ์ถํด์ MemoryMemberRepository๋ฅผ ์์ฑํ๊ณ ์คํ๋ง ์ปจํ
์ด๋์ ๋ฑ๋ก
return ๋ฐํ
}
}
-
@Bean์ด ๋ถ์ ๋ฉ์๋๋ง๋ค ์ด๋ฏธ ์คํ๋ง ๋น์ด ์กด์ฌํ๋ฉด, ์กด์ฌํ๋ ๋น์ ๋ฐํํ๊ณ , ์๋ค๋ฉด ์์ฑํด์ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํ๊ณ ๋ฐํํ๋ ์ฝ๋๊ฐ ๋์ ์ผ๋ก ๋ง๋ค์ด์ง๋ค.- ์ด ๋๋ถ์ ์ฑ๊ธํค์ด ๋ณด์ฅ๋๋ค.
AppConfig@CGLIB๋AppConfig์ ์์ ํ์ ์ด๋ค. ๋ฐ๋ผ์ ๋ถ๋ชจ ํ์ ์ธAppConfigํ์ ์ผ๋ก ์กฐํ๊ฐ ๊ฐ๋ฅํ๋ ๊ฒ์ด๋ค.
๐ช @Configuration ์์ด @Bean๋ง ์ ์ฉํ๋ฉด ์ด๋ป๊ฒ ๋ ๊น?
@Configuration ์ ๋ถ์ฌ ๋ฐ์ดํธ์ฝ๋๋ฅผ ์กฐ์ํ๋ CGLIB ๊ธฐ์ ์ ์ฌ์ฉํด์ ์ฑ๊ธํค์ ๋ณด์ฅํ์ง๋ง, @Bean ๋ง ์ ์ฉํ๋ฉด ์ด๋ป๊ฒ ๋ ๊น?
@Configuration์ญ์ ํ, ๋ค์ ์ถ๋ ฅ์ ๋ณด๋ฉดbean = class hello.core.AppConfig์ ๊ฐ์ด ์์ํAppConfig๋ก ์คํ๋ง ๋น์ ๋ฑ๋ก๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
ํ์ง๋ง! ์ฑ๊ธํค์ด ๋ณด์ฅ๋์ง ์์, ๋น ๋ฑ๋ก ์ ํธ์ถ๋๋ ์ถ๋ ฅ๋ฌธ์ด ์ด 5๋ฒ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. (MemberRepository ๊ฐ ์ด 3๋ฒ ๋ชจ๋ ํธ์ถ๋จ)
- ๋น์ฐํ ๋ชจ๋ ๋ค๋ฅธ ์ธ์คํด์ค๋ก ์์ฑ๋ ๊ฒ์ด๋ค!!!
์คํ๋ง ์ค์ ์ ๋ณด๋ ํญ์
@Configuration์ ๋ช ์ํ์ฌ ์ฑ๊ธํค์ ๋ณด์ฅํ๋๋ก ํ์.
๐ ์ค์ํ ๊ฐ๋
์ฑ๊ธํค ํจํด, ๋ฌด์ํ ์ค๊ณ, @Configuration